/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment_cp3120.DAO.mysqlDAO;

import assignment_cp3120.DAO.DestinationDAO;
import assignment_cp3120.dataObjects.Destination;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

/**
 *
 * @author Richard Irons
 */
public class MysqlDestinationDAO implements DestinationDAO{
    public Vector<Destination> getDestLocations() {
        Vector<Destination> destLoc = new Vector<Destination>();
        try{            
            Connection c = MysqlDA0Factory.createConnection();
            Statement statement = c.createStatement();
            ResultSet rs = statement.executeQuery(
                    "SELECT * FROM DEPARTURE_LOCATION");
            if(rs.isBeforeFirst()){
                rs.next();
            }            
            if(rs.first()){
                do {
                    Destination tempDepLoc = new Destination();
                    tempDepLoc.setDest_id(rs.getString("DEP_ID"));
                    tempDepLoc.setDest_name(rs.getString("DEP_NAME"));
                }
                while(rs.next());
            }            
            c.close();
        }catch(SQLException e){System.out.println(e.getMessage());}        
        return destLoc;
    }

    public String[] getDestNames() {
        Vector<String> destLocArray = new Vector<String>();
        try{            
            Connection c = MysqlDA0Factory.createConnection();
            Statement statement = c.createStatement();
            ResultSet rs = statement.executeQuery(
                    "SELECT DISTINCT DEP_NAME FROM DEPARTURE_LOCATION");
            if(rs.isBeforeFirst()){
                rs.next();
            }            
            if(rs.first()){
                while(rs.next()){
                    destLocArray.add(rs.getString("DEP_NAME"));
                }
            }            
            c.close();
        }catch(SQLException e){System.out.println(e.getMessage());}        
        String[] destLoc = new String[destLocArray.size()];
        for (int i = 0; i < destLocArray.size(); i++) {
            destLoc[i] = destLocArray.get(i);
        }
        return destLoc;
    }

    public int createDestination(Destination destination) {
        Connection c = MysqlDA0Factory.createConnection();
        int result = 0;
        try{
            Statement statment = c.createStatement();
            result = statment.executeUpdate(
                    "INSERT INTO 3120S109g09.DESTINATION ( "
                    + "DEST_ID, "
                    + "DEST_NAME "
                    + ") VALUES ( "
                    + "\"" + destination.getDest_id() + "\", "
                    + "\"" + destination.getDest_name() + "\" "
                    + ")");

        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
        return result;
    }

    public int deleteDestination(String id) {
        Connection c = MysqlDA0Factory.createConnection();
        int result = 0;
        try{
            Statement statment = c.createStatement();
            result = statment.executeUpdate(
                    "DELETE FROM `DESTINATION` WHERE DEST_ID = \""
                    +id
                    +"\" LIMIT 1 ");

        }catch(SQLException e){
            System.out.println(e.getMessage());
        }
        return result;
    }
}
